import {Component, Input, OnInit} from '@angular/core';
// @ts-ignore
import Moment from 'moment';
import {DateType} from '../enums';
import {DhlCommonInputComponent} from '../dhlCommonInput';
import {ObjectUtil} from '../../../utils/object-util';


@Component({
  selector: 'dhl-date',
  templateUrl: './dhl-date.component.html',
})
export class DhlDateComponent extends DhlCommonInputComponent implements OnInit {


  isShowTime;
  dateFormat = 'yyyy/MM/dd';

  @Input() type = '';
  @Input() placeholder;
  @Input() nzErrorTip;
  @Input() disabledDates;
  @Input() allowClear = true;

  @Input()
  set showTime(value) {
    this.isShowTime = value;
    this.dateFormat = value ? 'yyyy/MM/dd HH:mm:ss' : 'yyyy/MM/dd';
  }

  get showTime() {
    return this.isShowTime;
  }


  constructor() {
    super();
  }


  ngOnInit() {
    if (this.type == DateType.RANGE) {
      $('[dhl-input-element]', this.inputControl.nativeElement).attr('isArray', 'true');
    }
  }

  openClose() {

  }


  onChange(obj) {
    let formatV = this.isShowTime ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD';
    if (this.type == DateType.MONTH) {
      formatV = 'YYYY-MM';
    } else if (this.type == DateType.YEAR) {
      formatV = 'YYYY';
    }

    this.inputValue = obj ? Moment(obj.getTime()).format(formatV) : '';
    this.valueChange.emit(this.inputValue);
  }

  rangeOnChange(obj) {
    const formatV = this.isShowTime ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD';
    this.inputValue = obj.map(item => item ? Moment(item.getTime()).format(formatV) : null);
    this.valueChange.emit(this.inputValue);
  }


}


